package day1130;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;



public class StudentInfoTest {
	static Scanner sc = new Scanner(System.in);
	ArrayList<StudentInfo> list = new ArrayList<>();

	public static void main(String[] args) {
		new StudentInfoTest().start();
	}

	public void start() {
		while (true) {
			System.out.println("-----------");
			System.out.println("---请选择功能----");
			System.out.println("----1-录入学生信息--");
			System.out.println("----2-查看全部学生信息--");
			System.out.println("----3-按学生姓名查询学生信息--");
			System.out.println("----4-按专业查询学生信息--");
			System.out.println("----5-按学号删除学生信息--");
			System.out.println("----6-退出系统--");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				inputs();
				break;
			case 2:
				printAll();
				break;
			case 3:
				getStudentByName();
				break;
			case 4:
				getStudentByMajor();
				break;
			case 5:
				deleteStudentById();
				break;
			case 6: 
				return ;
			}
		}
	}

	//按照学号删除学生
	public void deleteStudentById() {
		System.out.println("请输入即将删除的学号： ");
		String id = sc.nextLine();
		int res = new StudentDao().delete(id);
		if(res==0)
		System.out.println("不存在学号为"+id+"的学生");
		else
			System.out.println("已删除");
	}
	
	//录入学生信息
	public void inputs() {
		while (true) {
			input();
			System.out.println("是否继续录入？y/n ");
			if (!"y".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}
	public void input() {

		StudentInfo student = new StudentInfo();
		String id = new String();
		System.out.println("请输入学生学号");
		do {
			id = sc.nextLine();
		} while (isExist(id));
		student.setId(id);
		System.out.println("请输入学生姓名");
		String name = sc.nextLine();
		System.out.println("请输入学生专业");
		String major = sc.nextLine();
		StudentInfo s = new StudentInfo(id,name,major);
		new StudentDao().insert(s);
	}

	public boolean isExist(String id) {
		
		ArrayList<StudentInfo> list = new StudentDao().selectID(id);
		if(!list.isEmpty()) {
			System.out.println("该学号已经存在");
			return true;
		}
		return false;
	}

	//按照专业查询学生
	public void getStudentByMajor() {
		System.out.println("请输入即将查询的专业：");
		String major = sc.nextLine();
		boolean flag = false;
		ArrayList<StudentInfo> list = new StudentDao().selectMajor(major);
		for(StudentInfo s : list) {
			flag = true;
			System.out.println(s.toString());
		}
			
		if (!flag)
			System.out.println("暂时没有这个专业的学生");
	}

	//查询所有学生信息
	public void printAll() {
		System.out.println("以下为所有学生信息：");
		ArrayList<StudentInfo> list = new StudentDao().selectAll();
		for(StudentInfo s : list)
			System.out.println(s.toString());
	}

	//通过名字查询学生信息
	public void getStudentByName() {
		System.out.println("请输入学生姓名：");
		String name = sc.nextLine();
		boolean flag = false;
		ArrayList<StudentInfo> list = new StudentDao().selectName(name);
		for(StudentInfo s : list) {
			flag = true;
			System.out.println(s.toString());
		}
		if (!flag)
			System.out.println("暂时没有这个名字的学生");
	}

}
